/*
 * Galaxium Messenger
 * 
 * Copyright (C) 2009 Philippe Durand <draekz@gmail.com>
 * 
 * License: GNU General Public License (GPL)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Collections.Generic;
using System.Security.Cryptography;

using Anculus.Core;

namespace Galaxium.Protocol.Facebook
{
	public class RequestErrorEventArgs : EventArgs
	{
		private string _message;
		
		public string Message { get { return _message; } }
		
		public RequestErrorEventArgs (string message)
		{
			_message = message;
		}
	}
	
	public abstract class AbstractRequest
	{
		public event EventHandler Completed;
		public event EventHandler<RequestErrorEventArgs> Failed;
		
		internal struct RequestState
		{
			public HttpWebRequest Request;
			public Stream Stream;
			public byte[] PostData;
			public string PostString;
		}
		
		protected FBConnection _connection;
		
		protected FBSession Session
		{
			get { return _connection.Session as FBSession; }
		}
		
		protected CookieCollection SavedCookies
		{
			get { return _connection.SavedCookies; }
		}
		
		public AbstractRequest(FBConnection connection)
		{
			_connection = connection;
		}
		
		public abstract void Begin ();
		
		protected void OnFailed (RequestErrorEventArgs args)
		{
			if (Failed != null)
				Failed (this, args);
		}
		
		protected void OnCompleted ()
		{
			if (Completed != null)
				Completed (this, new EventArgs());
		}
		
		protected HttpWebRequest CreateHttpRequest (string method, string url)
		{
			HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create (url);
			
			switch (method)
			{
			case "POST":
				request.ContentType = "application/x-www-form-urlencoded";
				break;
			case "GET":
				break;
			}
			
			request.Method = method;
			request.CookieContainer = new CookieContainer ();
			request.Accept = "*/*";
			request.UserAgent = "Opera/9.50 (Windows NT 5.1; U; en-GB)";
			request.KeepAlive = false;
			//request.AllowAutoRedirect = false;
			
			return request;
		}
		
		protected HttpWebRequest CreateHttpRequest (string method, string url, bool secure)
		{
			string host = "www.facebook.com";
			//string host = "69.63.186.11";
			//string host = "69.63.184.142";
			
			string finalUrl = String.Empty;
			
			if (secure)
				finalUrl += "https://"+host+url;
			else
				finalUrl += "http://"+host+url;
			
			HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create (finalUrl);
			
			switch (method)
			{
			case "POST":
				request.ContentType = "application/x-www-form-urlencoded";
				break;
			case "GET":
				break;
			}
			
			// Set GET or POST.
			request.Method = method;
			
			// Create a place to put cookies for this request.
			request.CookieContainer = new CookieContainer ();
			
			// Setup some defaults for each http request we make.
			request.Accept = "*/*";
			request.UserAgent = "Opera/9.50 (Windows NT 5.1; U; en-GB)";
			request.KeepAlive = false;
			request.AllowAutoRedirect = false;
			
			return request;
		}
	}
}
